﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace praca_v2
{
    public partial class Logowanie : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        private bool Zaloguj(string dbTableName, string connectionString = "ConnectionString")
        {
            ConnectionStringSettings settings = System.Configuration.ConfigurationManager.ConnectionStrings[connectionString];
            SqlConnection connection = new SqlConnection(settings.ConnectionString);
            connection.Open();

            //SqlCommand liczbaWierszy = new SqlCommand("SELECT count(*) as ilosc FROM " + dbTableName, connection);
            //int iloscWierszy = Convert.ToInt32(liczbaWierszy.ExecuteScalar());
            //string[] tabEmail = new string[iloscWierszy];

            SqlCommand command = new SqlCommand("SELECT * FROM " + dbTableName, connection);
            SqlDataReader dr = command.ExecuteReader();
            bool sprEmail = false;
            while (dr.Read())
            {
                if (TextBoxEmail.Text.Trim() == dr["Email"].ToString().Trim())
                {
                    if (TextBoxHaslo.Text.Trim() == dr["Haslo"].ToString().Trim())
                    {
                        sprEmail = true;
                    }
                }
            }
            connection.Close();
            return sprEmail;
        }

        protected void ButtonZaloguj_Click(object sender, EventArgs e)
        {
            if (Zaloguj("Klienci"))
            {
                Response.Write("<script>alert('Mision complited')</script>");

            }
            else
            {
                Response.Write("<script>alert('Logowanie sie nie powiodlo')</script>");
            }
        }
    }
}